package ru.poomsae.isap.Online.Freestyle

import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import com.example.isap_wtf.Model.ApiEndPoint
import com.example.isap_wtf.Model.ParametrsISAP
import com.example.isap_wtf.Model.zaprosJSON
import ru.poomsae.isap.Offline.MenuActivity
import ru.poomsae.isap.R

class Freestyle : AppCompatActivity() {

    var hie = 0
    var num = 0
    var ang = 0
    var spa = 0
    var acr = 0
    var bas = 0

    var levelHeight = 0
    var countKick = 0
    var realiCountKick = 0
    var angelCount = 0
    var acrobaticLevel = 0
    var acrobaticKick = 0

    var accy = 0.0
    var accyInt = 0

    var min01 = 0
    var min03 = 0

    var url = ""
    var mIDHost: String = ParametrsISAP.idHostServer
    var onlineConSys = ParametrsISAP.onlineConSys
    lateinit var myzaprosJSON: zaprosJSON
    lateinit var myTextView: TextView
    lateinit var myLinearLayout: LinearLayout
    lateinit var myRelativeLayout: RelativeLayout
    lateinit var mySudTextView: TextView
    lateinit var myPoomsaeTextView: TextView
    var idvistuplenie = "null"
    lateinit var myheightLinearLayout: LinearLayout
    lateinit var heightText: TextView
    lateinit var realiCickText: TextView
    lateinit var realiCoucntCickLayout: LinearLayout
    lateinit var realiCountCick: RadioGroup
    lateinit var countCick: RadioGroup

    lateinit var progressBarFree: LinearLayout
    lateinit var idvistFreeText: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.freestyle)

        url = "http://" + mIDHost + ApiEndPoint.VISTUPLENIE

        myTextView = findViewById(R.id.idvistFreeText) as TextView
        myLinearLayout = findViewById(R.id.progressBarFree) as LinearLayout
        myRelativeLayout= findViewById(R.id.freestyleRelativeLayout) as RelativeLayout
        mySudTextView = findViewById(R.id.sudNumFreeTV) as TextView
        myPoomsaeTextView = findViewById(R.id.poomsaeFreeTV) as TextView
        myheightLinearLayout = findViewById(R.id.heightLinearLayout) as LinearLayout
        heightText = findViewById(R.id.heightText)

        realiCickText = findViewById(R.id.realiCickText)
        realiCoucntCickLayout = findViewById(R.id.realiCoucntCickLayout)
        realiCountCick = findViewById(R.id.realiCountCick)
        countCick = findViewById(R.id.countCick)

        progressBarFree = findViewById(R.id.progressBarFree)
        idvistFreeText = findViewById(R.id.idvistFreeText)

        myzaprosJSON = zaprosJSON(myTextView, myLinearLayout, myRelativeLayout, mySudTextView, myPoomsaeTextView)
        val decorView = window.decorView
        val uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN

        if(onlineConSys == 1) { myzaprosJSON.waitVistup(); visibleSystem(); decorView.systemUiVisibility = uiOptions; getSupportActionBar()?.hide() }

        val btnFree01 = findViewById<Button>(R.id.btnFree01)
        val tFree01 = findViewById<TextView>(R.id.tFree01)
        val tFree03 = findViewById<TextView>(R.id.tFree03)
        val accyrFree = findViewById<TextView>(R.id.accyrFree)

        btnFree01.setOnClickListener {
            accyInt = (accy * 10).toInt()
            if (accyInt > 0) {
                accyInt = accyInt - 1
                min01 = min01 + 1
                tFree01.setText("x " + min01.toString())
                accy = accyInt.toDouble()/10.toDouble()
                accyrFree.setText(String.format("%.1f",accy))
            }

            if (accyInt < 0) {
                accy = 0.0
                accyrFree.setText(accy.toString())
            }
        }

        val btnFree03 = findViewById<Button>(R.id.btnFree03)
        btnFree03.setOnClickListener {
            accyInt = (accy * 10).toInt()
            if (accyInt > 0) {
                accyInt = accyInt - 3
                min03 = min03 + 1
                tFree03.setText(min03.toString() + " x")
                accy = accyInt.toDouble()/10.toDouble()
                accyrFree.setText(String.format("%.1f",accy))
            }

            if (accyInt < 0) {
                accy = 0.0
                accyrFree.setText(accy.toString())
            }
        }

        val btnCanFree01 = findViewById<Button>(R.id.btnCanFree01)
        btnCanFree01.setOnClickListener {
            if (min01 > 0) {
                accyInt = accyInt + 1
                min01 = min01 - 1
                tFree01.setText("x " + min01.toString())
                accy = accyInt.toDouble()/10.toDouble()
                accyrFree.setText(String.format("%.1f",accy))
            }
        }

        val btnCanFree03 = findViewById<Button>(R.id.btnCanFree03)
        btnCanFree03.setOnClickListener {
            if (min03 > 0) {
                accyInt = accyInt + 3
                min03 = min03 - 1
                tFree03.setText(min03.toString() + " x")
                accy = accyInt.toDouble()/10.toDouble()
                accyrFree.setText(String.format("%.1f",accy))
            }
        }

        val btnResFree = findViewById<Button>(R.id.btnResFree)
        btnResFree.setOnClickListener { accy = 0.0; min01 = 0; min03 = 0; accyrFree.setText(String.format("%.1f",accy)); tFree03.setText(min03.toString() + " x"); tFree01.setText("x " + min01.toString())  }

        val level = findViewById<RadioGroup>(R.id.level)
        level.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                myheightLinearLayout.visibility = View.VISIBLE
                heightText.visibility = View.VISIBLE
                levelHeight = (radio.text as String).toInt()
            })

        val numberCickText = findViewById<TextView>(R.id.numberCickText)
        val numberCickLinear = findViewById<LinearLayout>(R.id.numberCickLinear)

        realiCountCick.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                numberCickText.visibility = View.VISIBLE
                numberCickLinear.visibility = View.VISIBLE
                realiCountKick = (radio.text as String).toInt()
            })


        val height = findViewById<RadioGroup>(R.id.height)
        val heigFree = findViewById<TextView>(R.id.heigFree)
        height.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                hie = ((radio.text as String).toDouble() * (10).toDouble()).toInt()
                accy = (hie + num + ang + spa + acr + bas).toDouble()/(10).toDouble()
                heigFree.setText(radio.text as String)
                accyrFree.setText(String.format("%.1f",accy))
            })

        countCick.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                realiCickText.visibility = View.VISIBLE
                realiCoucntCickLayout.visibility = View.VISIBLE
                countKick = (radio.text as String).toInt()
            })

        val kick = findViewById<RadioGroup>(R.id.kick)
        val numkickFree = findViewById<TextView>(R.id.numkickFree)
        kick.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                num = ((radio.text as String).toDouble() * (10).toDouble()).toInt()
                accy = (hie + num + ang + spa + acr + bas).toDouble()/(10).toDouble()
                numkickFree.setText(radio.text as String)
                accyrFree.setText(String.format("%.1f",accy))
            })

        val angleText = findViewById<TextView>(R.id.angleText)
        val angleLinear = findViewById<LinearLayout>(R.id.angleLinear)
        val angleRadio = findViewById<RadioGroup>(R.id.angleRadio)

        angleRadio.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                angleText.visibility = View.VISIBLE
                angleLinear.visibility = View.VISIBLE
                angelCount = (radio.text as String).toInt()
            })

        val angle = findViewById<RadioGroup>(R.id.angle)
        val angleFree = findViewById<TextView>(R.id.angleFree)
        angle.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                ang = ((radio.text as String).toDouble() * (10).toDouble()).toInt()
                accy = (hie + num + ang + spa + acr + bas).toDouble()/(10).toDouble()
                angleFree.setText(radio.text as String)
                accyrFree.setText(String.format("%.1f",accy))
            })

        val sparing = findViewById<RadioGroup>(R.id.sparing)
        val sparFree = findViewById<TextView>(R.id.sparFree)
        sparing.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                spa = ((radio.text as String).toDouble() * (10).toDouble()).toInt()
                accy = (hie + num + ang + spa + acr + bas).toDouble()/(10).toDouble()
                sparFree.setText(radio.text as String)
                accyrFree.setText(String.format("%.1f",accy))
            })

        val levelAcro = findViewById<RadioGroup>(R.id.levelAcro)
        val acrobCickText = findViewById<TextView>(R.id.acrobCickText)
        val ackrobCickLayout = findViewById<LinearLayout>(R.id.ackrobCickLayout)
        levelAcro.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                acrobCickText.visibility = View.VISIBLE
                ackrobCickLayout.visibility = View.VISIBLE
                acrobaticLevel = (radio.text as String).toInt()
            })

        val ackrobCick = findViewById<RadioGroup>(R.id.ackrobCick)
        val acrobaticText = findViewById<TextView>(R.id.acrobaticText)
        val acrobaticLinear = findViewById<LinearLayout>(R.id.acrobaticLinear)
        ackrobCick.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                acrobaticText.visibility = View.VISIBLE
                acrobaticLinear.visibility = View.VISIBLE
                acrobaticKick = (radio.text as String).toInt()
            })

        val acrobatic = findViewById<RadioGroup>(R.id.acrobatic)
        val acrFree = findViewById<TextView>(R.id.acrFree)
        acrobatic.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                acr = ((radio.text as String).toDouble() * (10).toDouble()).toInt()
                accy = (hie + num + ang + spa + acr + bas).toDouble()/(10).toDouble()
                acrFree.setText(radio.text as String)
                accyrFree.setText(String.format("%.1f",accy))
            })

        val basic = findViewById<RadioGroup>(R.id.basic)
        val basFree = findViewById<TextView>(R.id.basFree)
        basic.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                bas = ((radio.text as String).toDouble() * (10).toDouble()).toInt()
                accy = (hie + num + ang + spa + acr + bas).toDouble()/(10).toDouble()
                basFree.setText(radio.text as String)
                accyrFree.setText(String.format("%.1f",accy))
            })

        val btnPresentationFree = findViewById<Button>(R.id.btnPresentationFree)
        btnPresentationFree.setOnClickListener {
            val intent = Intent (this, FreestylePresentation::class.java)
            intent.putExtra("accuracyDoubleFree", accy)
            intent.putExtra("levelHeight", levelHeight)
            intent.putExtra("countKick", countKick)
            intent.putExtra("realiCountKick", realiCountKick)
            intent.putExtra("angelCount", angelCount)
            intent.putExtra("acrobaticLevel", acrobaticLevel)
            intent.putExtra("acrobaticKick", acrobaticKick)

            intent.putExtra("hie", hie)
            intent.putExtra("num", num)
            intent.putExtra("ang", ang)
            intent.putExtra("spa", spa)
            intent.putExtra("acr", acr)
            intent.putExtra("bas", bas)
            startActivity(intent)
        }


    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_off, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item?.itemId) {

            R.id.menu_offline -> {
                val intent = Intent (this, MenuActivity::class.java)
                startActivity(intent)
            }

        }

        return true
    }

    fun visibleSystem() {
        if (idvistuplenie == "null") {
            myRelativeLayout.visibility = View.GONE
            progressBarFree.visibility = View.VISIBLE
            idvistFreeText.setText(getString(R.string.idvistTextView))
        }
    }
}